#!/usr/bin/python

# Python script for Nepomuk firefox

#Get the URL of current Web page


import logging
logger = logging.getLogger("mgrComm")


logger.debug("Script mgrComm.py in the position")


current_url = window.opener.content.document.location.href


def getPageURL():

	try:
		watchedGetPageURL()
	except Exception, e:
		PROMPTS.alert(window, "Error", "Problem accessing the needed elements to use Nepomuk Foxtrot. Please check that nepomukserver is up and running (Error is \"%s\")." % e)
		window.close()



def watchedGetPageURL():
	
	# attach TheNepomukWhisperer to the document
	from the_nepomuk_whisperer import TheNepomukWhisperer
	document.NEPO = TheNepomukWhisperer()
	
	# get the page url
	document.getElementById("bt-savecomm").disabled = True
	document.getElementById("labelPageURL").setAttribute("value",current_url)
	logger.debug(current_url)
	getComment()


def checkInput():
		document.getElementById("bt-savecomm").disabled = (document.getElementById("tb-comment").value=="")


def clearText():
	tagtext=document.getElementById("tb-comment")
	tagtext.setAttribute("value","")
	document.getElementById("bt-savecomm").disabled = True


def tagSummary():
#Get the input
	commentText = document.getElementById("textcomment").value
	
	document.getElementById("labComment").setAttribute("value",commentText)


#Save the Comment
def saveComment():
	
	comm2save = document.getElementById("tb-comment").value
	logger.debug('The description is added')
	
	document_uri = document.NEPO.interface.getAntecedent(PIMO_GROUNDINGOCCURRENCE, current_url)
	if document_uri != None:
		logger.debug("The document %s has %s as its groundingOccurrence." % (document_uri, current_url))
	else:
		logger.debug("The page \"%s\" has no groundingOccurrence, creating one." % current_url)
		document_uri = document.NEPO.interface.registerAndReify(current_url, NFO_WEBSITE, window.opener.title)
	
	document.NEPO.nao.setDescription(document_uri, comm2save)
	
	window.close()


def removeComment():
	
	document_uri = document.NEPO.interface.getAntecedent(PIMO_GROUNDINGOCCURRENCE, current_url)
	if document_uri == None:
		return 1
	
	document.NEPO.nao.removeDescription(document_uri)
	document.getElementById("tb-comment").setAttribute("value","The comment has been removed!")


#get comment
def getComment():
	
	document_uri = document.NEPO.interface.getAntecedent(PIMO_GROUNDINGOCCURRENCE, current_url)
	if document_uri == None:
		return "no description"
	
	commentText=document.NEPO.nao.getDescriptionOf(document_uri)
	document.getElementById("tb-comment").setAttribute("value",commentText)


def onUnload():
	
	#Something hope to WORK
	event_handler = components.classes["@mozilla.org/EventHandlers;1"].getService(components.interfaces.nsIEventHandlers);
	event_handler.refreshPageInfo(current_url, window.opener.document)

	#window.opener.getElementById("sidebar").getAttribute("contentDocument").getElementById("SBcommentIcon").setAttribute("tooltiptext", "theComment");








